import UserService from '@/services/user.service.js'
import DocumentService from '@/services/document.service'

export const state = () => ({
  userId: null,
  drawer: null,
  flag: null
})

export const mutations = {
  setFlag(state, flag) {
    state.flag = flag
  },
  setUserId(state, userId) {
    state.userId = userId
  },
  setDrawer(state) {
    state.drawer = !state.drawer
  }
}

export const actions = {
  getUserId({
    commit
  }) {
    return UserService.getMe()
      .then((res) => {
        localStorage.setItem('isAdmin', res.data.is_superuser)
        commit('setUserId', res.data.id)
      })
  },
  getFlag({ commit }, payload) {
    return DocumentService.getTopLevelLabel(payload)
      .then((res) => {
        commit('setFlag', res.data.data)
      })
  }
}
